#include "ProductDataAccess.h"

// 商品显示功能
QList<Product> ProductDataAccess::getProducts(const QString &type, const QString &keyword, const QString &filterCriteria, const QString &sort)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    QString sql = "SELECT product_id, product_name, price, stock, sales, date, description FROM Products WHERE type = :type";
    QList<Product> products;

    // 添加过滤条件
    if (!filterCriteria.isEmpty())
    {
        sql += " AND Filter_Criteria = :filter_Criteria";
    }

    // 添加关键词搜索条件
    if (!keyword.isEmpty())
    {
        sql += " AND (product_name LIKE :keyword OR Description LIKE :keyword)";
    }

    // 添加排序条件
    if (sort == "新品" or sort == "日期")
    {
        sql += " ORDER BY Date DESC";
    }
    else if (sort == "价格")
    {
        sql += " ORDER BY Price ASC";
    }
    else if (sort == "销量")
    {
        sql += " ORDER BY Sales DESC";
    }

    // 准备sql语句
    query.prepare(sql);

    query.bindValue(":type", type);

    if (!filterCriteria.isEmpty())
    {
        query.bindValue(":filter_Criteria", filterCriteria);
    }

    if (!keyword.isEmpty())
    {
        query.bindValue(":keyword", "%" + keyword + "%");
    }

    // 开始查询
    if (!query.exec())
    {
        qDebug() << "获取排序商品失败：" << query.lastError().text();
        return products; // 返回空列表
    }

    while (query.next())
    {
        Product product;

        // 设置 Products 类的属性
        product.productId = query.value("product_id").toInt();
        product.productName = query.value("Product_Name").toString();
        product.price = query.value("Price").toDouble();
        product.stock = query.value("Stock").toInt();
        product.sales = query.value("Sales").toInt();
        product.date = query.value("Date").toDate();
        product.description = (query.value("Description").toString());

        product.image = FileConnector::getProductImage(product.productId);

        products.append(product);
    }

    return products;
}

// 添加商品评论  addProductReview
bool ProductDataAccess::addProductReview(const int &productId, const int &userId, const QString &content)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    query.prepare("INSERT INTO ProductReviews (product_id, user_id, content, review_date) VALUES (?, ?, ?, ?)");
    query.addBindValue(productId);
    query.addBindValue(userId);
    query.addBindValue(content);
    query.addBindValue(QDateTime::currentDateTime());

    if (!query.exec())
    {
        qDebug() << "插入评论失败：";
        return false;
    }

    return true;
}

// 获取商品评论
QList<ProductReview> ProductDataAccess::getReviews(const int &productId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    query.prepare("SELECT product_id, product_name, price, stock, sales, date, description FROM Reviews WHERE productId = ?");
    query.addBindValue(productId);

    QList<ProductReview> productreviews;

    if (query.exec())
    {
        while (query.next())
        {
            ProductReview productreview;
            productreview.reviewId = query.value("review_id").toInt();
            productreview.productId = query.value("productId").toInt();
            productreview.userId = query.value("UserID").toInt();
            productreview.content = query.value("Content").toString();
            productreview.reviewDate = query.value("ReviewDate").toDate();

            productreviews.append(productreviews);
        }
    }
    else
    {
        qDebug() << "获取评论失败：" << query.lastError();
    }

    return productreviews;
}

Product ProductDataAccess::getProductById(const int &productId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    query.prepare("SELECT product_id, product_name, price FROM Products WHERE product_id = ?");
    query.addBindValue(productId);

    Product product;

    if (query.exec())
    {
        if (query.next())
        {
            product.productId = query.value("product_id").toInt();
            product.productName = query.value("Product_Name").toString();
            product.price = query.value("Price").toDouble();
            product.image = FileConnector::getProductImage(product.productId);
        }
    }
    else
    {
        qDebug() << "根据ID获取商品失败：" << query.lastError();
    }

    return product;
}